<template>
  <div class="InvitationCode" :style="'min-height: ' + docheight + 'px'">
    <div v-if="!showTimeOut && showCont">
      <div class="codeUserCont">
        <div class="codeUserTitle">
          <div>接单记录</div>
          <div class="timeBtn">
            <span v-if="startDate && endDate">
              <span @click="showDate = true">{{ startDate }}～{{ endDate }}</span>
              <span style="color:#6455d4" @click="czTime">重置日期</span>
            </span>
            <span v-else>订单日期:<span @click="showDate = true" style="color:#6455d4">未选择</span></span>
          </div>
        </div>
        <div class="codeUserList">
          <div class="codeUserItem" :key="index" v-for="(item, index) in userArr">
            <div class="topCont">
              <img :src="item.course_cover" />
              <div class="userInfo">
                <div class="userName">
                  <span class="desTag pinkTag" v-if="item.order_status == 3">待服务</span>
                  <span class="desTag greenTag" v-if="item.order_status == 5">已完成</span>
                  <span class="desTag blacktag" v-if="item.order_status == 7">已取消</span>
                  <span>{{ item.course_name }}</span>
                </div>
                <div class="priceCont">
                  <div class="priceAll">最低到手：¥{{ item.teacher_commission }}</div>
                </div>
                <div class="userTime">预约日期：{{ item.sk_date }}</div>
                <div class="userTime">预约时间：{{ item.sk_time }}</div>
              </div>
            </div>
            <div class="underCont">
              <div class="goodsType">
                <div class="goodsTypeItem">总计授课：{{ item.num }}小时</div>
              </div>
              <div class="orderBtn sureBtn" v-if="item.order_status == 3" @click="showClock(item)">立即打卡</div>
            </div>
            <div class="remarkVal">打卡情况:{{ item.clock_num }}/{{ item.num }}</div>
            <div class="remarkVal">服务地址:{{ item.service_address }} {{ item.house_number }}</div>
            <div class="remarkVal flexText">
              <div>联系电话:{{ item.user_phone }}({{ item.user_name }})</div>
              <div class="telText" @click="telFn(item.user_phone)" v-if="item.showPhone">拨号</div>
            </div>
            <div class="remarkVal">订单备注:{{ item.service_remark ? item.service_remark : '无备注' }}</div>
            <div class="moreCont">
              <div class="setCarCont" v-if="item.show">
                <div class="setCarList" :key="index2" v-for="(item2, index2) in item.clock_record">
                  <div class="setCarItem">
                    <div class="formName">打卡时间：</div>
                    <div>{{ item2.create_time }}</div>
                  </div>
                  <div class="setCarItem">
                    <div class="formName">打卡数量：</div>
                    <div>{{ parseInt(item2.clock_num) }}（{{ getUnit(item) }}）</div>
                  </div>
                </div>
              </div>
              <div class="moreBtn" v-if="!item.show" @click="item.show = true">展开打卡记录</div>
              <div class="moreBtn" v-if="item.show" @click="item.show = false">收起打卡记录</div>
            </div>
          </div>
          <div class="nodataTip" v-if="userArr.length == 0">
            你还没有订单记录哦～
          </div>
          <div class="nodataTip" v-if="userArr.length > 0">{{ loadMoreText }}</div>
        </div>
      </div>
      <sub-load v-if="showLoad"></sub-load>
    </div>
    <van-calendar ref="calend" :min-date="minDate" :max-date="maxDate" color="#6455d4" v-model:show="showDate"
      type="range" @confirm="onConfirm" />
    <van-popup v-model:show="goodsMask" round position="bottom">
      <div class="buyMaskCont">
        <div class="activeTip">请上传打卡凭证</div>
        <div class="setCarImg">
          <defUpImg propImg="" @upImgSuccess="getImg" icon="icon-xiangji2"></defUpImg>
        </div>
        <div class="activeTip">请选择打卡数量</div>
        <div class="checkNum">
          <div :class="goodsNum == 1 ? 'disBtn editBtn iconfont icon-jian1' : 'editBtn iconfont icon-jian1'" @click="downNum">
          </div>
          <div class="goodsnum">{{ goodsNum }}</div>
          <div class="editBtn iconfont iconfont icon-jia" @click="upNum"></div>
        </div>
        <div class="activeTip" style="margin-top: 1rem;">*请正确打卡，违规胡乱打卡会有惩罚哦~</div>
        <div class="maskBtn">
          <div class="canBuy" @click="clockFn"><span class="iconfont icon-shandian"></span>确认打卡</div>
        </div>
      </div>
    </van-popup>
    <toast v-model="showToast" position="middle" width="50%" type="text" :time="1000">{{ showText }}</toast>
    <footcpm></footcpm>
    <getLoad v-if="!showCont"></getLoad>
    <getTimeOut v-if="showTimeOut"></getTimeOut>
  </div>
</template>

<script>
import { Toast } from "vux";
import SaveBtn from "@/components/public/saveBtn.vue";
import subLoad from "@/components/public/subLoad.vue";
import getLoad from "@/components/public/getLoad";
import getTimeOut from "@/components/public/getTimeOut";
import footcpm from "@/components/footcpm";
import { recordOrder, clock } from "@/http/api";
import defUpImg from '@/components/public/defUpImg.vue'
import bus from '@/assets/evnetBus.js'

export default {
  components: { getTimeOut, getLoad, subLoad, Toast, SaveBtn, footcpm, defUpImg },
  data() {
    return {
      showTimeOut: false,
      showCont: false,
      showLoad: false,
      showText: "",
      loadMoreText: '数据加载中～',
      showToast: false,
      showDate: false,
      userArr: [],
      minDate: new Date(2022, 0, 1),
      maxDate: new Date(),
      startDate: "",
      endDate: "",
      page: 1,
      per_page: 4,
      last_page: 0,
      getting: false,
      goodsNum: 1,
      goodsMask: false,
      voucher: '',
      order_id: '',
    };
  },
  props: {},
  methods: {
    getUnit(item) {
      if (item.course_unit == 1) {
        return '小时'
      } else if (item.course_unit == 2) {
        return '课时'
      }
    },
    getTimeStr(time) {
      let n = time.getFullYear();
      let y = time.getMonth() + 1;
      let d = time.getDate();
      let timeStr = n + "-" + y + "-" + d;
      return timeStr;
    },
    getImg(str) {
      this.voucher = str
      console.log(str)
    },
    telFn(num) {
      window.location.href = "tel:" + num
    },
    upNum() {
      this.goodsNum++
    },
    downNum() {
      if (this.goodsNum > 1) {
        this.goodsNum--
      }
    },
    czTime() {
      this.endDate = "";
      this.startDate = "";
      this.page = 1;
      this.last_page = 0;
      this.userArr = [];
      this.$refs.calend.reset();
      this.getData();
    },
    onConfirm(values) {
      const [start, end] = values;
      this.startDate = this.getTimeStr(values[0]);
      this.endDate = this.getTimeStr(values[1]);
      this.showDate = false;
      this.userArr = [];
      this.page = 1;
      this.last_page = 0;
      this.getData();
    },
    getData() {
      let time = "";
      if (this.startDate != "" && this.endDate != "") {
        time = this.startDate + "," + this.endDate;
      }
      let data = {
        page: this.page,
        per_page: this.per_page,
        time: time
      };
      recordOrder(data)
        .then(res => {
          this.showCont = true;
          if (res.data.code == 1) {
            let arr = res.data.data.data
            for (let i = 0; i < arr.length; i++) {
              arr[i]['show'] = false
              arr[i]['showPhone'] = true
              if (arr[i].user_phone.indexOf('*')>-1) {
                arr[i]['showPhone'] = false
              }
              this.userArr.push(arr[i])
            }
            console.log(this.userArr)
            setTimeout(() => {
              this.getting = false
            }, 100);
            this.last_page = res.data.data.last_page
            if (this.page == this.last_page) {
              this.loadMoreText = '到底啦～'
            } else {
              this.loadMoreText = '数据加载中~'
            }
          } else {
            this.showToast = true;
            this.showText = res.data.msg
          }
        })
        .catch(() => {
          this.showTimeOut = true;
          this.showCont = true;
        });
    },
    showClock(item) {
      this.order_id = item.order_id
      this.goodsMask = true
    },
    clockFn() {
      let data = {
        clock_num: this.goodsNum,
        order_id: this.order_id,
        voucher: this.voucher
      }
      if (this.voucher == '') {
        this.showToast = true;
        this.showText = '请上传打卡凭证'
        return false
      }
      if (!this.showLoad) {
        this.goodsMask = false
        this.showLoad = true
        clock(data).then((res) => {
          if (res.data.code == 1) {
            this.showToast = true;
            this.showText = res.data.msg
            this.userArr = [];
            this.page = 1;
            this.last_page = 0;
            this.getData()
            bus.$emit('getWait')
          } else {
            this.showToast = true;
            this.showText = res.data.msg
          }
          this.showLoad = false
        }).catch(() => {
          this.showTimeOut = true;
          this.showCont = true;
          this.showLoad = false
        });
      }

    },
    nextfn() {
      if (this.$route.path == "/history") {
        let h = document.documentElement.scrollHeight || document.body.scrollHeight;
        let wh = document.documentElement.clientHeight || document.body.clientHeight;
        let top = document.documentElement.scrollTop || document.body.scrollTop;
        let bottom = h - wh - top;
        if (bottom <= 50) {
          if (this.getting == false) {
            if (this.last_page > this.page) {
              this.getting = true;
              this.page++;
              this.getData();
            }
          }
        }
      }
    }
  },
  mounted() {
    window.addEventListener("scroll", this.nextfn);
  },
  created() {
    this.getData();
  }
};
</script>

<style scoped lang="less">
.InvitationCode {
  width: 100%;
  background-color: #f7f8fa;
  box-sizing: border-box;
  padding-bottom: 3.5rem;
  padding-top: 1rem;

  .codeUserCont {
    width: 92%;
    margin: 0 auto;
    // background-color: #fff;
    // padding: 0.75rem;
    box-sizing: border-box;

    .codeUserTitle {
      font-size: 0.8rem;
      font-weight: bold;
      padding: 0.75rem;
      border-radius: 0.25rem;
      box-sizing: border-box;
      background-color: #fff;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .timeBtn {
        font-weight: normal;
        font-size: 0.65rem;
      }
    }

    .codeUserList {
      padding: 0.75rem 0;

      .codeUserItem {
        width: 100%;
        background-color: #fff;
        padding: 0.75rem;
        border-radius: 0.25rem;
        box-sizing: border-box;
        margin-bottom: 0.75rem;

        .topCont {
          display: flex;
          padding: 0.5rem 0;
          align-items: center;

          img {
            margin-right: 0.75rem;
            width: 4.5rem;
            border-radius: 5px;
          }

          .userInfo {
            width: 2.5rem;
            flex: 1;

            .userName {
              font-size: 0.7rem;
              font-weight: bold;
              color: #333;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              width: 100%;

              .desTag {
                padding: 0.1rem 0.5rem;
                background-color: #6455d4;
                font-size: 0.6rem;
                color: #fff;
                border-radius: 0.1rem;
                font-weight: normal;
              }

              .pinkTag {
                color: #fff;
                background-color: #F56C6C;
              }

              .greenTag {
                background-color: #85CE61;
              }

              .blacktag {
                color: #666;
                background-color: #ebebeb;
              }
            }

            .priceCont {
              align-items: center;
              display: flex;
              font-size: 0.6rem;
              color: #666;
              margin-top: 0.5rem;

              .priceSub {
                margin-left: 0.75rem;
              }
            }

            .goodsType {
              display: flex;
              align-items: center;
              justify-content: space-between;
              margin-top: 0.35rem;

              .goodsNumCont {
                display: flex;
                height: 1rem;
                line-height: 1rem;
                color: #999;
                font-size: 0.6rem;
                align-items: center;
              }

              .goodsTypeItem {
                padding: 0.1rem 0.5rem;
                background-color: #6455d4;
                font-size: 0.6rem;
                height: 1rem;
                line-height: 1rem;
                border-radius: 0.75rem;
                color: #fff;
              }
            }

            .userTime {
              font-size: 0.6rem;
              margin-top: 0.25rem;
              color: #666;
            }
          }
        }

        .underCont {
          display: flex;
          align-items: center;
          justify-content: space-between;

          .goodsType {
            display: flex;
            align-items: center;
            justify-content: space-between;

            .goodsNumCont {
              display: flex;
              height: 1rem;
              line-height: 1rem;
              color: #999;
              font-size: 0.6rem;
              align-items: center;
            }

            .goodsTypeItem {
              padding: 0.1rem 0.5rem;
              background-color: #6455d4;
              font-size: 0.6rem;
              height: 1rem;
              line-height: 1rem;
              border-radius: 0.75rem;
              color: white;
            }
          }

          .orderBtn {
            padding: 0.35rem 1.8rem;
            border: 1px solid #6455d4;
            color: #6455d4;
            font-size: 0.6rem;
            margin-left: 0.5rem;
            border-radius: 0.25rem;
          }

          .sureBtn {
            background-color: #6455d4;
            color: white;
          }
        }

        .remarkVal {
          color: #666;
          font-size: 0.6rem;
          margin-top: 0.5rem;
        }

        .flexText {
          display: flex;
          justify-content: space-between;
          align-items: center;

          .telText {
            color: #6455d4;
            text-decoration: underline;
          }
        }

        .moreCont {
          font-size: 0.6rem;
          margin-top: 0.75rem;

          .setCarCont {
            width: 100%;

            .setCarList {
              padding: 0.5rem 0;
              color: #666;
              font-size: 0.6rem;
              border-block: 1px dashed #ebebeb;

              .setCarItem {
                display: flex;
                margin-bottom: 0.25rem;
                justify-content: space-between;
              }
            }
          }

          .moreBtn {
            text-align: center;
            padding-top: 0.75rem;
            color: #666;
            text-decoration: underline;
            border-top: 1px dashed #ebebeb;
          }
        }
      }

      .nodataTip {
        color: #666;
        font-size: 0.6rem;
        text-align: center;
        margin-top: 20px;
      }
    }
  }

  .orderMask {
    padding: 0.75rem;
    box-sizing: border-box;

    .maskTitle {
      font-size: 0.8rem;
      font-weight: bold;
      text-align: center;
    }

    .maskTip {
      margin-top: 0.5rem;
      line-height: 1.1rem;
    }
  }

  .buyMaskCont {
    width: 100%;
    padding: 1rem 4%;
    box-sizing: border-box;
    background-color: #fff;


    .activeTip {
      font-size: 0.6rem;
      color: #666;
      text-align: center;
      margin-bottom: 0.75rem;
    }

    .setCarImg {
      width: 100%;
      display: flex;
      justify-content: center;
    }

    .checkNum {
      width: 100%;
      display: flex;
      justify-content: center;

      .editBtn {
        width: 2.5rem;
        height: 2.5rem;
        font-size: 1.5rem;
        text-align: center;
        line-height: 2.5rem;
        background-color: #c2bbf9;
        color: #fff;
        border-radius: 50%;
      }

      .disBtn {
        background-color: #ebebeb;
        color: white;
      }

      .goodsnum {
        height: 2.5rem;
        width: 5rem;
        line-height: 2.5rem;
        text-align: center;
        font-size: 1.5rem;
      }
    }

    .maskBtn {
      // margin: 1.25rem 0 0.75rem;
      margin-top: 0.75rem;
      width: 100%;
      border-radius: 0.25rem;
      overflow: hidden;
      display: flex;

      .buyGoods,
      .canBuy {
        width: 100%;
        font-size: 0.7rem;
        text-align: center;
        background-color: #6455d4;
        color: white;
        padding: 0.75rem 0;
      }

      .buyGoods {
        opacity: 0.5;
      }
    }
  }
}</style>
<style>.InvitationCode .van-cell::after {
  border-bottom: none;
}</style>
